package codeRandomThoughts.Test309最佳买卖股票时机含冷冻期;

public class Solution {
    public int maxProfit(int[] prices) {
        if (prices.length == 1) {
            return 0;
        }

        //dp[i][0]:前i天不持有股票获得的最大利润
        //dp[i][1]:前i天持有股票获得的最大利润
        int[][] dp = new int[prices.length][2];

        //初始化
        dp[0][0] = 0;
        dp[0][1] = -prices[0];
        dp[1][0] = Math.max(dp[0][1] + prices[1], dp[0][0]);
        dp[1][1] = Math.max(-prices[1], dp[0][1]);

        for (int i = 2; i < prices.length; i++) {
            dp[i][0] = Math.max(dp[i - 1][1] + prices[i], dp[i - 1][0]);
            //此处我们要拿之前的收益减去prices[i]
            dp[i][1] = Math.max(dp[i - 2][0] - prices[i], dp[i - 1][1]);
        }

        return dp[prices.length - 1][0];
    }
}

